/**************************************************************************//**
*
* @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author       Notes
* 2020-12-12      Wayne        First version
*
******************************************************************************/

#include <rtconfig.h>
#include <rtdevice.h>

#define TEST_CAN_NUM 4

static void can_rx_thread(void *arg);
static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size);
static struct rt_semaphore rx_sem[TEST_CAN_NUM];
static rt_thread_t can_thread_id[TEST_CAN_NUM] = {RT_NULL};

static void can_rx_thread(void *arg)
{
    int can_idx = (int)arg;

    RT_ASSERT(can_idx >= 0 &&  can_idx < TEST_CAN_NUM);

    char can_name[8];
    rt_snprintf(can_name, sizeof(can_name)-1, "can%d", can_idx);

    rt_err_t res = rt_sem_init(&rx_sem[can_idx], can_name, 0, RT_IPC_FLAG_FIFO);
    RT_ASSERT(res == RT_EOK);

    rt_device_t can_dev = rt_device_find(can_name);
    RT_ASSERT(can_dev);

    res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
    RT_ASSERT(res == RT_EOK);

    res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void *)CAN500kBaud);
    RT_ASSERT(res == RT_EOK);

    res = rt_device_control(can_dev, RT_CAN_CMD_SET_MODE, (void *)RT_CAN_MODE_NORMAL);
    RT_ASSERT(res == RT_EOK);

    #ifdef RT_CAN_USING_HDR
    struct rt_can_filter_item items[4] =
    {
        {0x000, 0, 0, 0, 0x700, 0, CAN_RX_FIFO0, RT_NULL, RT_NULL},
        {0x100, 0, 0, 0, 0x700, 1, CAN_RX_FIFO0, RT_NULL, RT_NULL},
        {0x200, 0, 0, 0, 0x700, 2, CAN_RX_FIFO0, RT_NULL, RT_NULL},
        {0x300, 0, 0, 0, 0x700, 3, CAN_RX_FIFO0, RT_NULL, RT_NULL},
    };
    struct rt_can_filter_config cfg = {4, 1, items};
    res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
    RT_ASSERT(res == RT_EOK);
    #endif

    res = rt_device_set_rx_indicate(can_dev, can_rx_call);
    RT_ASSERT(res == RT_EOK);

    while(1) {
        struct rt_can_msg rxmsg = {0};
        rt_sem_take(&rx_sem[can_idx], RT_WAITING_FOREVER);
        rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
        rt_kprintf("can[%d] receive ID:0x%x", can_idx, rxmsg.id);
        for (int i = 0; i < rxmsg.len; i++)
        {
            rt_kprintf("%02x", rxmsg.data[i]);
        }
        rt_kprintf("\n");
    }
}

static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
{
    int can_idx = dev->parent.name[3] - '0';
    RT_ASSERT(can_idx >= 0 &&  can_idx < TEST_CAN_NUM);
    rt_sem_release(&rx_sem[can_idx]);
    return RT_EOK;
}

int can_test(int argc, char *argv[])
{
    struct rt_can_msg msg = {0};
    rt_err_t res = 0;
    rt_size_t  size;
    char can_name[RT_NAME_MAX];

    for(int i = 0; i < TEST_CAN_NUM; i ++) {
        if (!can_thread_id[i])
        {
            can_thread_id[i] = rt_thread_create("can_test", can_rx_thread, (void *)i, 1024, 12, 10);
            RT_ASSERT(can_thread_id[i]);
            rt_thread_startup(can_thread_id[i]);
            rt_thread_delay(200);
        }
    }

    if (argc == 2)
    {
        rt_strncpy(can_name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(can_name, "can0", RT_NAME_MAX);
    }

    /* 查找 CAN 设备 */
    rt_device_t can_dev = rt_device_find(can_name);
    if (!can_dev)
    {
        rt_kprintf("find %s failed!\n", can_name);
        return RT_ERROR;
    }

    msg.ide = RT_CAN_STDID;     /* 标准格式 */
    msg.rtr = RT_CAN_DTR;       /* 数据帧 */
    msg.len = 8;                /* 数据长度为 8 */
    msg.data[0] = 0x00;
    msg.data[1] = 0x11;
    msg.data[2] = 0x22;
    msg.data[3] = 0x33;
    msg.data[4] = 0x44;
    msg.data[5] = 0x55;
    msg.data[6] = 0x66;
    msg.data[7] = 0x77;

    for (int i = 0; i < 4 ; i ++)
    {
        msg.id = 0x78 + (0x100 * i);              /* ID 为 0x78 */
        size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
        if (size == 0)
        {
            rt_kprintf("can dev write data failed, mssgid: 0x%x!\n", msg.id);
        }
        rt_thread_mdelay(200);
    }


    return res;
}
MSH_CMD_EXPORT(can_test, can send test);
